package com.agw.crm.workbench.service.impl;

import com.agw.crm.settings.dao.UserDao;
import com.agw.crm.settings.domain.User;
import com.agw.crm.utils.SqlSessionUtil;
import com.agw.crm.vo.PaginationVo;
import com.agw.crm.workbench.dao.ActivityDao;
import com.agw.crm.workbench.dao.ActivityRemarkDao;
import com.agw.crm.workbench.domain.Activity;
import com.agw.crm.workbench.domain.ActivityRemark;
import com.agw.crm.workbench.service.ActivityService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ActivityServiceImpl implements ActivityService {

    private ActivityDao dao = SqlSessionUtil.getSqlSession().getMapper(ActivityDao.class);
    private ActivityRemarkDao remarkDao = SqlSessionUtil.getSqlSession().getMapper(ActivityRemarkDao.class);
    private UserDao userDao = SqlSessionUtil.getSqlSession().getMapper(UserDao.class);
    @Override
    public boolean save(Activity activity) {
        boolean flag = true;

        int sum = dao.save(activity);
        if (sum !=1){
            flag = false;
        }
        return flag;
    }

    @Override
    public PaginationVo<Activity> pageList(Map<String, Object> map) {
        //查询记录总条数
        int total = dao.getTotalByCondition(map);
        //查询所有的activity记录
        List<Activity> list = dao.getActivityListByCondition(map);
        //将上面的结果封装到vo对象中
        PaginationVo<Activity> vo = new PaginationVo<Activity>();
        vo.setTotal(total);
        vo.setDataList(list);
        //返回vo
        return vo;
    }

    @Override
    public boolean delete(String[] ids) {
        //定义一个boolean类型做接受
        boolean flag = true;

        //查询删除备注信息的数量
        int count1 = remarkDao.getCountsByIds(ids);
        //删除备注信息数量
        int count2 = remarkDao.deleteByIds(ids);
        if (count1!=count2){
            flag = false;
        }
        //删除市场活动
        int count3 = dao.delete(ids);
        if (count3 != ids.length){
            flag = false;
        }
        return flag;
    }

    @Override
    public Map<String, Object> getUserListAndActivity(String id) {

        //查询所有的所有者
        List<User> userList = userDao.getUserList();
        //根据id查询单条记录
        Activity activity = dao.getActivityById(id);
        //将数据封装到map集合中
        Map<String,Object> map = new HashMap<>();
        map.put("userList",userList);
        map.put("activity",activity);
        return map;
    }

    @Override
    public boolean update(Activity activity) {
        boolean flag = true;

        int sum = dao.update(activity);
        if (sum !=1){
            flag = false;
        }
        return flag;
    }

    @Override
    public Activity detail(String id) {
        //调用dao层查询数据
        Activity activity = dao.detail(id);

        return activity;
    }

    @Override
    public List<ActivityRemark> getRemarkListByAid(String activityId) {
        List<ActivityRemark> list = remarkDao.getRemarkListByAid(activityId);

        return list;
    }

    @Override
    public boolean deleteRemark(String id) {

        boolean flag = true;

        int sum = remarkDao.deleteRemark(id);

        if (sum != 1){
           flag = false;
        }

        return flag;
    }

    @Override
    public boolean saveRemark(ActivityRemark ar) {
        boolean flag = true;

        int count = remarkDao.saveRemark(ar);

        if (count!=1){
            flag = false;
        }
        return flag;
    }

    @Override
    public boolean updateRemark(ActivityRemark ar) {

        boolean flag = true;

        int count = remarkDao.updateRemark(ar);
        if (count!=1){
            flag = false;
        }
        return flag;
    }

    @Override
    public List<Activity> getActivityListByClueId(String clueId) {
        List<Activity> aList = dao.getActivityListByClueId(clueId);
        return aList;
    }

    @Override
    public List<Activity> getActivityListByNameAndNotByClueId(Map<String, String> map) {
        List<Activity> aList = dao.getActivityListByNameAndNotByClueId(map);
        return aList;
    }

    @Override
    public List<Activity> getActivityListByName(String aname) {
        List<Activity> aList = dao.getActivityListByName(aname);
        return aList;
    }


}
